#Region " Imports "

Imports System
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Net.Configuration
Imports System.IO
Imports System.Data
Imports BlazeApps.Library.Forum

#End Region

Partial Class Usercontrols_Forum_addpost
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private ForumCheck As New ForumChecks

    Protected Property TopicGuid() As Guid
        Get
            Return GetGuidFromVS("TopicID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("TopicID", value)
        End Set
    End Property

    Protected Property PostGuid() As Guid
        Get
            Return GetGuidFromVS("PostID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("PostID", value)
        End Set
    End Property

    Protected Property EditPost() As Boolean
        Get
            Return GetBooleanFromVS("editpost")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("editpost", value)
        End Set
    End Property

    Protected Property AttachItem() As Boolean
        Get
            Return GetBooleanFromVS("AttachItem")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("AttachItem", value)
        End Set
    End Property

    Protected Property QuotePost() As Boolean
        Get
            Return GetBooleanFromVS("quote")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("quote", value)
        End Set
    End Property

    Protected Property ForumModerated() As Boolean
        Get
            Return GetBooleanFromVS("ForumModerated")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("ForumModerated", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Post Message")
        GetQueryStringValues()

        If ForumCheck.IsForumLocked(Guid.Empty, TopicGuid) And (Not IsAdmin()) Then
            Me.uxPostPanel.Visible = False
            Me.uxMessagesLabel.Text = Resources.Resource.ForumLocked.ToString().Trim()
        Else
            If IsUserAuthenticated() Then

                ConfigBaseEditorCtrls()

                If Me.Page.IsPostBack = False Then
					BuildFCKEditor(EditorPageType.ForumEditor)
                    ForumModerated = ForumCheck.IsForumModerated(Guid.Empty, TopicGuid)
                    If EditPost Or QuotePost Then
                        GetPostDetails()
                    End If

                End If

            End If

            LoadSettings()

            Me.uxPostPanel.Visible = ShowControls(SecurityMask.Member, True)
        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SavePost()
        Dim Item As Post = Post.NewPost
        Dim ForumCheck As New ForumChecks

		Item.PostBody = Me.uxAddPostTextbox.Value
		Item.UserID = CurrentUserID
		Item.TopicID = TopicGuid
		If IsModerator() And ForumModerated Then
			Item.IsApproved = True
		Else
			Item.IsApproved = Not ForumModerated
		End If
		Item.Save()

	End Sub

	Private Sub UpdatePost()
		Dim Item As Post = Post.GetPost(PostGuid)
		Dim ForumCheck As New ForumChecks

		Item.PostBody = Me.uxAddPostTextbox.Value
		Item.TopicID = TopicGuid
		Item.EditedString = DateTime.Now.ToString
		If IsModerator() And ForumModerated Then
			Item.IsApproved = True
		Else
			Item.IsApproved = Not ForumModerated
		End If
		Item.Save()

	End Sub

#End Region

#Region " Load Data "

	Private Sub LoadSettings()
		uxMyGalleryTabPanel.Enabled = (EnableUserGalleries OrElse IsAdmin())
		uxAttachmentsTabPanel.Enabled = (EnableUserGalleries OrElse IsAdmin())

		Me.uxAttachmentsTabPanel.Enabled = AttachItem
		Me.uxMyGalleryTabPanel.Enabled = AttachItem

	End Sub

	Private Sub GetPostDetails()
		Dim Item As Post = Post.GetPost(PostGuid)

		If QuotePost Then
			Me.uxAddPostTextbox.Value = String.Format("<p><center><table class=""FORUM_QUOTED""><tr><td class=""FORUM_QUOTED_USER_CELL"">{1} Said:<br /></td></tr><tr><td  class=""FORUM_QUOTED_BODY_CELL"">{0}</td></tr></table></center></p>", Item.PostBody, Membership.GetUser(Item.UserID).UserName.ToString)
		Else
			Me.uxAddPostTextbox.Value = Item.PostBody
		End If

	End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click

        If EditPost Then
            UpdatePost()
        Else
            SavePost()

            If EnableEmailNotifications Then
                GenerateNotificationEmails()
            End If
        End If

		Session.Add("page", GetIntegerFromQueryString("page"))

		Response.Redirect(String.Format("{0}.aspx?pid=for_post&topicid={1}", AppPage, TopicGuid))

    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        TopicGuid = GetGuidFromQueryString("topicid")
        PostGuid = GetGuidFromQueryString("postid")
        EditPost = GetBooleanFromQueryString("editpost")
        QuotePost = GetBooleanFromQueryString("quote")
        AttachItem = GetBooleanFromQueryString("attachitem")
    End Sub

    Private Sub ConfigBaseEditorCtrls()
		Me.FCKEditor = Me.uxAddPostTextbox
    End Sub

    Private Overloads Sub GenerateNotificationEmails()

        Dim EmailList As New TopicList
        Dim DT As New DataTable
        Dim DR As DataRow
        Dim PU As New ProfileUtility

        DT = EmailList.GetTopicEmailDetails(TopicGuid)

        For Each DR In DT.Rows

            If PU.ReceiveEmailNotification(Membership.GetUser(ConvertToGuid(DR("userid"))).UserName.ToString) Then

                Dim ToAddress As String = Membership.GetUser(ConvertToGuid(DR("userid"))).Email
                Dim FromAddress As String = MailSettingsSection.Smtp.From.ToString().Trim()
                Dim EmailBody As New StringBuilder
                Dim Subject As String = String.Format("{0} {1}", Resources.Resource.PostReplySubject, DR("title"))

                EmailBody.Append(Resources.Resource.AutoGeneratedEmail)
                EmailBody.Append("<br>")
                EmailBody.Append(Resources.Resource.PostReplyBody)
                EmailBody.Append("<br>")
                EmailBody.Append(String.Format("<a href=""http://{0}{1}{2}"">{3}</a>", Request.ServerVariables("HTTP_HOST"), Request.ServerVariables("URL"), DR("urlparams"), DR("title")))
                EmailBody.Append("<br>")
                EmailBody.Append(String.Format("<a href=""http://{0}{1}"">{2}</a>", Request.ServerVariables("HTTP_HOST"), Request.ServerVariables("URL"), SiteTitle))
                EmailBody.Append("<br>")
                EmailBody.Append("<br>")
                EmailBody.Append(String.Format("<a href=""http://{0}{1}?pid=cp_profile"">{2}</a>", Request.ServerVariables("HTTP_HOST"), Request.ServerVariables("URL"), Resources.Resource.UnsubscribeFromEmails))

                SendEmail(ToAddress, "", "", FromAddress, Subject, EmailBody.ToString().Trim(), True)
            End If

        Next

    End Sub

#End Region

End Class
